/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.bandwidth;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.factories.ButtonBarFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.util.Date;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UnsupportedLookAndFeelException;
import org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.textgroup.IntegerValidator;
import tnviet.proj.jna.textgroup.MaskTextField;
import tnviet.proj.jna.textgroup.TextGroup;
import tnviet.proj.jna.utilities.ResourceManager;

/**
 *
 * @author Administrator
 */
public class BlockFilterPane extends JDialog{

    private ResourceManager resourceManager = new ResourceManager(FilterTableFrame.class, Program.locale);
    String[] protocolName = {resourceManager.getString("any"),
                "ICMP", "TCP", "UDP"};
    Integer[] protocolValue = {0,1,6,17};
    JSpinner protocolValueSpinner;
    JRadioButton fromAnyPortRdb, fromThisPortRdb, toAnyPortRdb, toThisPortRdb;
    MaskTextField fromPortField, toPortField;
    JTextArea descriptionArea;
    ButtonGroup fromGroup, toGroup;
    JButton okButton, cancelButton;
    JComboBox sourceAddressList, destAddressList, protocolList;
    TextGroup sourceAdd, sourceSubnet, destAdd, destSubnet;
    FilterInfo filter = null;
    int option;
    
    class RadioButtonPortListener implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                String cmd = e.getActionCommand();
                if(cmd.equals("FromAnyPort")){
                    fromPortField.setEnabled(false);
                } else if(cmd.equals("FromThisPort")){
                    fromPortField.setEnabled(true);
                } else if(cmd.equals("ToAnyPort")){
                    toPortField.setEnabled(false);
                } else if(cmd.equals("ToThisPort")){
                    toPortField.setEnabled(true);
                }
            }
    };
    public int getOption(){
        return option;
    }


    public BlockFilterPane(Frame owner, boolean modal) {
        super(owner, modal);
        this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        String[] address = {
            resourceManager.getString("anyAddress"),
            resourceManager.getString("myAddress"),
            resourceManager.getString("specificIP"),
            resourceManager.getString("specificSubnet")
        };
        sourceAddressList = new JComboBox(address);
        sourceAddressList.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox)e.getSource();
                int value = cb.getSelectedIndex();
                if(value == 0 || value == 1){
                    sourceAdd.setEnabledRecursively(false);
                    sourceSubnet.setEnabledRecursively(false);
                } else if (value == 2){
                    sourceAdd.setEnabledRecursively(true);
                    sourceSubnet.setEnabledRecursively(false);
                } else if(value == 3){
                    sourceAdd.setEnabledRecursively(true);
                    sourceSubnet.setEnabledRecursively(true);
                }
            }
        });
        destAddressList = new JComboBox(address);
        destAddressList.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox)e.getSource();
                int value = cb.getSelectedIndex();
                if(value == 0 || value == 1){
                    destAdd.setEnabledRecursively(false);
                    destSubnet.setEnabledRecursively(false);
                } else if (value == 2){
                    destAdd.setEnabledRecursively(true);
                    destSubnet.setEnabledRecursively(false);
                } else if (value == 3){
                    destAdd.setEnabledRecursively(true);
                    destSubnet.setEnabledRecursively(true);
                }
            }
        });

        // ip textbox
        IntegerValidator ipv = new IntegerValidator(0, 255);
        IntegerValidator numeric = new IntegerValidator();
        sourceAdd = new TextGroup("   .   .   .   ", ".", ipv);
        sourceAdd.setText("1.1.1.1");
        sourceAdd.setEnabledRecursively(false);
        sourceSubnet = new TextGroup("   .   .   .   ", ".", ipv);
        sourceSubnet.setText("255.255.255.255");
        sourceSubnet.setEnabledRecursively(false);
        destAdd = new TextGroup("   .   .   .   ", ".", ipv);
        destAdd.setText("1.1.1.1");
        destAdd.setEnabledRecursively(false);
        destSubnet = new TextGroup("   .   .   .   ", ".", ipv);
        destSubnet.setText("255.255.255.255");
        destSubnet.setEnabledRecursively(false);

        // protocol
        RadioButtonPortListener radioButtonPortListener = new RadioButtonPortListener();
        protocolList = new JComboBox(protocolName);
        SpinnerNumberModel spinnerModel = new SpinnerNumberModel(0, 0, 255, 1);
        protocolValueSpinner = new JSpinner(spinnerModel);
        protocolValueSpinner.setValue(protocolValue[protocolList.getSelectedIndex()]);
        protocolValueSpinner.setEnabled(false);
        protocolList.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox)e.getSource();
                int index = cb.getSelectedIndex();
                protocolValueSpinner.setValue(protocolValue[cb.getSelectedIndex()]);
                if(index == 9){
                    protocolValueSpinner.setEnabled(true);
                } else {
                    protocolValueSpinner.setEnabled(false);
                }
            }
        });

        // Port
        fromAnyPortRdb = new JRadioButton(resourceManager.getString("fromAnyPort"));
        fromAnyPortRdb.setSelected(true);
        fromAnyPortRdb.setActionCommand("FromAnyPort");
        fromAnyPortRdb.addActionListener(radioButtonPortListener);
        toAnyPortRdb = new JRadioButton(resourceManager.getString("toAnyPort"));
        toAnyPortRdb.setSelected(true);
        toAnyPortRdb.setActionCommand("ToAnyPort");
        toAnyPortRdb.addActionListener(radioButtonPortListener);
        fromThisPortRdb = new JRadioButton(resourceManager.getString("fromThisPort"));
        fromThisPortRdb.setActionCommand("FromThisPort");
        fromThisPortRdb.addActionListener(radioButtonPortListener);
        toThisPortRdb = new JRadioButton(resourceManager.getString("toThisPort"));
        toThisPortRdb.setActionCommand("ToThisPort");
        toThisPortRdb.addActionListener(radioButtonPortListener);
        fromPortField = new MaskTextField(10, "");
        fromPortField.setValidator(numeric);
        fromPortField.setEnabled(false);
        toPortField = new MaskTextField(10, "");
        toPortField.setValidator(numeric);
        toPortField.setEnabled(false);
        descriptionArea  = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(descriptionArea);
        descriptionArea.setRows(3);
        fromGroup = new ButtonGroup();
        fromGroup.add(fromAnyPortRdb);
        fromGroup.add(fromThisPortRdb);
        toGroup = new ButtonGroup();
        toGroup.add(toAnyPortRdb);
        toGroup.add(toThisPortRdb);

        // button
        okButton = new JButton(resourceManager.getString("okButton"));
        okButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String source_mask = sourceSubnet.getText();
                String dest_mask = destSubnet.getText();
                String source_Add = sourceAdd.getText();
                if(sourceAddressList.getSelectedIndex() == 0){
                    source_Add = "*";
                    source_mask = "0";
                }
                else if (sourceAddressList.getSelectedIndex() == 1){
                    source_Add = "0";
                    source_mask = "0";
                }
                String dest_Add = destAdd.getText();
                if(destAddressList.getSelectedIndex() == 0){
                    dest_Add = "*";
                    dest_mask = "0";
                }
                else if (destAddressList.getSelectedIndex() == 1){
                    dest_Add = "0";
                    dest_mask = "0";
                }
                
                String protocol_name = protocolName[protocolList.getSelectedIndex()];
                if(protocol_name.equals(resourceManager.getString("any"))) protocol_name = "0";
                int source_port  = 0,  dest_port = 0;
                if(fromThisPortRdb.isSelected() == true){
                    source_port = Integer.parseInt(fromPortField.getText());
                }
                if(toThisPortRdb.isSelected() == true){
                    dest_port = Integer.parseInt(toPortField.getText());
                }
                String description = descriptionArea.getText();
                Timestamp time = new Timestamp(new Date().getTime());
                filter = new FilterInfo(source_Add,
                        dest_Add, source_mask, dest_mask, protocol_name, source_port,
                        dest_port, description, false, time);
                option = JOptionPane.OK_OPTION;
                setVisible(false);
            }
        });

        cancelButton = new JButton(resourceManager.getString("cancelButton"));
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                option = JOptionPane.CANCEL_OPTION;
                setVisible(false);
            }
        });
        JPanel okCancelBar = ButtonBarFactory.buildOKCancelBar(okButton, cancelButton);

        FormLayout layout = new FormLayout("10dlu, pref, 5dlu, pref, 15dlu, pref, 5dlu, pref",
                "p, 2dlu, p, 2dlu, p, 2dlu, p, 8dlu, p, 2dlu, p, 8dlu, p, 2dlu, p, 2dlu, p, 8dlu" +
                ", p, 5dlu, pref, 8dlu, pref");
        PanelBuilder builder = new PanelBuilder(layout);
        CellConstraints cc = new CellConstraints();
        builder.addSeparator(resourceManager.getString("addressing"), cc.xyw(1, 1, 8));
        builder.addLabel(resourceManager.getString("source"), cc.xy(2, 3));
        builder.add(sourceAddressList, cc.xy(4, 3));
        builder.addLabel(resourceManager.getString("ipAddress"), cc.xy(2, 5));
        builder.add(sourceAdd, cc.xy(4, 5));
        builder.addLabel(resourceManager.getString("subnetMask"), cc.xy(2, 7));
        builder.add(sourceSubnet, cc.xy(4, 7));
        builder.addLabel(resourceManager.getString("dest"), cc.xy(6, 3));
        builder.add(destAddressList, cc.xy(8, 3));
        builder.addLabel(resourceManager.getString("ipAddress"), cc.xy(6, 5));
        builder.add(destAdd, cc.xy(8, 5));
        builder.addLabel(resourceManager.getString("subnetMask"), cc.xy(6, 7));
        builder.add(destSubnet, cc.xy(8, 7));
        builder.addSeparator(resourceManager.getString("protocolType"), cc.xyw(1, 9, 8));
        builder.addLabel(resourceManager.getString("protocolName"), cc.xy(2, 11));
        builder.add(protocolList, cc.xy(4, 11));
        builder.addLabel(resourceManager.getString("tos"), cc.xy(6, 11));
        builder.add(protocolValueSpinner, cc.xy(8, 11));
        builder.addSeparator(resourceManager.getString("setPort"), cc.xyw(1, 13, 8));
        builder.add(fromAnyPortRdb, cc.xy(2, 15));
        builder.add(toAnyPortRdb, cc.xy(6, 15));
        builder.add(fromThisPortRdb, cc.xy(2, 17));
        builder.add(fromPortField, cc.xy(4, 17));
        builder.add(toThisPortRdb, cc.xy(6, 17));
        builder.add(toPortField, cc.xy(8, 17));
        builder.addSeparator(resourceManager.getString("description"), cc.xyw(1, 19, 8));
        builder.add(scrollPane, cc.xyw(1, 21, 8));
        builder.add(okCancelBar, cc.xyw(1, 23, 8));
        builder.setDefaultDialogBorder();
        this.setTitle(resourceManager.getString("insertFilter"));
        this.getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
        this.setLocationRelativeTo(owner);
        this.setSize(this.getPreferredSize());
        this.pack();
    }

    public FilterInfo getFilter() {
        return filter;
    }
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(new SubstanceBusinessBlueSteelLookAndFeel());
                    JDialog.setDefaultLookAndFeelDecorated(true);
                    BlockFilterPane dialog = new BlockFilterPane(null, false);
                    dialog.setLocationRelativeTo(null);
                    dialog.setVisible(true);
                } catch (UnsupportedLookAndFeelException ex) {
                }
            }
        });
        //aa.setVisible(true);
    }

    
//    public static FilterInfo InsertFilter(Frame owner){
//        BlockFilterPane filterPane = new BlockFilterPane();
//        JDialog dialog = new JDialog(owner, "Block Filter Pane", true);
//        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//        dialog.getContentPane().add(filterPane, BorderLayout.CENTER);
//        dialog.pack();
//        //dialog.
//    }



}
